package cn.lyjuan.android.mylibrary.frame.load.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.lyjuan.android.mylibrary.R;


/**
 * 无权限显示页
 * Created by chad on 13/11/2017.
 */
public class LoadFailedFragmentImpl extends AbstractLoadFailedFragment
{
    /**
     * 状态提示文字
     */
    protected Integer stateTipId;

    /**
     * 状态提示图片
     */
    protected Integer imgId;

    /**
     * 生成错误Fragment实例
     *
     * @param stateTipId 错误状态提示文字
     * @param imgId      错误状态提示图片
     * @return
     */
    public static LoadFailedFragmentImpl newInstance(Integer stateTipId, Integer imgId)
    {
        LoadFailedFragmentImpl frag = new LoadFailedFragmentImpl();
        frag.setArguments(new Bundle());// 空bundle以存储
        return frag.setStateTipId(stateTipId).setImgId(imgId);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
    {
        super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_load_failed, container, false);

        ((TextView) view.findViewById(R.id.load_failed_state)).setText(stateTipId);
        ((ImageView) view.findViewById(R.id.load_failed_image)).setImageResource(imgId);

        // 重载临听
        view.findViewById(R.id.reload_btn).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (null != onReloadListener)
                    onReloadListener.onReload();
            }
        });

        return view;
    }

    @Override
    protected void store(Bundle ble)
    {
        super.store(ble);

        ble.putInt("load_state_tip_id", stateTipId);
        ble.putInt("load_image_id", imgId);
    }

    @Override
    protected void restore(Bundle ble)
    {
        super.restore(ble);

        if (null == stateTipId)
        {
            int tmp = ble.getInt("load_state_tip_id", -1);
            if (tmp > 0)
                stateTipId = tmp;
        }

        if (null == imgId)
        {
            int tmp = ble.getInt("load_image_id", -1);
            if (tmp > 0)
                imgId = tmp;
        }
    }

    public int getStateTipId()
    {
        return stateTipId;
    }

    public LoadFailedFragmentImpl setStateTipId(int stateTipId)
    {
        this.stateTipId = stateTipId;
        return this;
    }

    public int getImgId()
    {
        return imgId;
    }

    public LoadFailedFragmentImpl setImgId(int imgId)
    {
        this.imgId = imgId;
        return this;
    }
}
